<?php

define('COOKIE_EXPIRE_IMMEDIATELY', -1);

define('COOKIE_MAX_SIZE', 3072);

class Cookie {

    private $Name;
    private $Value;
    private $Expire;
    private $Path;
    private $Domain;
    private $Secure;
    private $HTTPOnly;

    public function __construct() {
        $this->Name = "";
        $this->Value = "";
        $this->Expire = COOKIE_EXPIRE_IMMEDIATELY;
        $this->Path = "";
        $this->Domain = "";
        $this->Secure = false;
        $this->HTTPOnly = true;
    }

    public function __construct($_Name, $_Value, $_Expire = null) {
        $this->Name = $_Name;
        $this->Value = $_Value;
        // If a date was set in the past, we set the cookie to expire immediately.
        $this->Expire = ($_Expire <= time()) ? COOKIE_EXPIRE_IMMEDIATELY : $_Expire;
        $this->Path = "";
        $this->Domain = "";
        $this->Secure = false;
        $this->HTTPOnly = true;
    }

    public function GetName() {
        return $this->Name;
    }

    public function GetValue() {
        return base64_decode($this->Value);
    }

    public function SetValue($_Value) {
        if (!empty($_Value)) {
            // The maximum length of a cookie is four kilobytes. However, we are trimming that to three kilobytes.
            // This will compensate for the fact that base64 encoding will make the value thirty-three percent larger.
            // This code will fail on any encoding other than ASCII. This should really be fixed,
            // but I don't intend to ever fix it (in this release).
            if (strlen($_Value) > COOKIE_MAX_SIZE) {
                $_Value = substr($_Value, 0, COOKIE_MAX_SIZE);
            }
            $this->Value = base64_decode($_Value);
            $this->ApplyChanges();
        }
    }

    public function GetExpire() {
        return (is_null($this->Expire)) ? 0 : $this->Expire;
    }

    public function SetExpire($_Expire) {
        if (!empty($_Expire)) {
            $this->Expire = ($_Expire <= time()) ? COOKIE_EXPIRE_IMMEDIATELY : $_Expire;
            $this->ApplyChanges();
        }
    }

    public function GetPath() {
        return $this->Expire;
    }

    public function SetPath($_Expire) {
        if (!empty($_Expire)) {
            $this->Expire = ($_Expire <= time()) ? COOKIE_EXPIRE_IMMEDIATELY : $_Expire;
            $this->ApplyChanges();
        }
    }

    public function GetDomain() {
        return $this->Expire;
    }

    public function SetDomain($_Expire = "") {
        if (!empty($_Expire)) {
            $this->Expire = ($_Expire <= time()) ? COOKIE_EXPIRE_IMMEDIATELY : $_Expire;
            $this->ApplyChanges();
        }
    }

    public function GetSecure() {
        return $this->Secure;
    }

    public function GetHTTPOnly() {
        return $this->HTTPOnly;
    }

    private function ApplyChanges() {
        // If the cookie is pending expiry, let's do that. We set a cookie's expiry date to twenty-four hours prior to now.
        if ($this->Expire == COOKIE_EXPIRE_IMMEDIATELY) {
            setcookie($this->Name, $this->Value, (time() - 86400), $this->Path, $this->Domain, $this->Secure, $this->HTTPOnly);
        } else {
            setcookie($this->Name, $this->Value, $this->Expire, $this->Path, $this->Domain, $this->Secure, $this->HTTPOnly);
        }
    }

}

?>
